package com.naiterui.ehp.ps.sms.factory;

import com.alibaba.fastjson.JSONObject;
import com.naiterui.ehp.ps.sms.enums.CloopenStatus;
import com.naiterui.ehp.ps.sms.exception.ExceptionCodes;
import com.naiterui.ehp.ps.sms.exception.SmsBusinessException;
import com.naiterui.ehp.ps.sms.vo.MessageResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 容联短信平台
 *
 * @author
 * @since 1.0.0
 */
@Slf4j
public class CloopenSms {

    /**
     * 单条发送短信
     * CloopenSms.siggleSend()
     *
     * @param phone
     *
     * @return
     *
     * @throws SmsBusinessException
     * @Author
     * @Date 2017年7月26日
     * @since 1.0.0
     */
    public static MessageResponseVO siggleSend(String phone, String templateId, String[] datas) throws SmsBusinessException {
        // 发送短信
        String result = CloopenApi.siggleSend(phone, templateId, datas);
        log.info("[发送短信，容联平台]--短信发送完成，手机号电话号码:[{}] 模板ID:[{}] 模板参数:[{}] 第三方平台返回结果:[{}]", phone, templateId, datas, result);
        MessageResponseVO messageResponseVO = processApiResult(result);
        return messageResponseVO;
    }

    /**
     * 处理API返回的结果
     * CloopenSms.processApiResult()
     *
     * @param result
     *
     * @return
     *
     * @throws SmsBusinessException
     * @Author
     * @Date 2017年7月26日
     * @since 1.0.0
     */
    private static MessageResponseVO processApiResult(String result) throws SmsBusinessException {
        // 处理api请求结果
        if (StringUtils.isEmpty(result)) {
            log.error("容联平台返回结果为空：[{}]", result);
            throw new SmsBusinessException(ExceptionCodes.SMS_PLATFORM_RETURN_NULL);
        }
        JSONObject resultJson = JSONObject.parseObject(result);
        MessageResponseVO responseVO = new MessageResponseVO();
        String statusCode = resultJson.getString("statusCode");
        if (CloopenStatus.SUCCESS.code.equals(statusCode)) {
            responseVO.setStatusCode(String.valueOf(ExceptionCodes.SUCCESS));
            responseVO.setStatusMsg("成功");
        } else {
            Integer covertCode = convertErrorCode(statusCode);
            if (covertCode != null) {
                log.error("容联平台返回状态码转换错误,statusCode：{}", statusCode);
                throw new SmsBusinessException(covertCode);
            } else {
                responseVO.setStatusCode(statusCode);
                responseVO.setStatusMsg(resultJson.getString("statusMsg"));
            }
        }

        return responseVO;
    }

    /**
     * 转换容联的返回错误码
     * CloopenSms.convertErrorCode()
     *
     * @param statusCode
     *
     * @return
     *
     * @Author
     * @Date 2017年8月4日
     * @since 1.0.0
     */
    private static Integer convertErrorCode(String statusCode) {
        if (CloopenStatus.TEMPLATE_TYPE_NOT_KNOW.code.equals(statusCode) || CloopenStatus.TEMPLATE_NOT_MATCH.code.equals(statusCode)
                || CloopenStatus.TEMPLATE_NOT_VALIAD.code.equals(statusCode) || CloopenStatus.TEMPLATE_VARIABLE_ERROR.code.equals(statusCode)
                || CloopenStatus.TEMPLATE_FORMAT_ERROR.code.equals(statusCode)) {
            // 未找到对应的平台模板
            return ExceptionCodes.SMS_PLATFORM_TEMPLATE_NULL;
        } else if (CloopenStatus.CONTENT_CONTAIN_FORBIDDEN_WORDS.code.equals(statusCode)
                || CloopenStatus.CONTENT_CONTAIN_SENSITIVE_WORDS.code.equals(statusCode)) {
            // 内容中包含敏感词,发送失败!
            return ExceptionCodes.SMS_CONTENT_CONTAIN_FORBIDDEN_WORDS;
        } else if (CloopenStatus.PHONE_REJECTED.code.equals(statusCode) || CloopenStatus.PHONE_INVALID.code.equals(statusCode)
                || CloopenStatus.PHONE_EMPTY.code.equals(statusCode)) {
            // 非法的手机号码
            return ExceptionCodes.SMS_PHONE_ERROR;
        } else if (CloopenStatus.CONCURRENT_OVER_LIMIT.code.equals(statusCode) || CloopenStatus.CAPTCHA_OVER_LIMIT.code.equals(statusCode)
                || CloopenStatus.TEMPLATE_OVER_LIMIT.code.equals(statusCode) || CloopenStatus.TEMPLATE_CAPTCHA_OVER_LIMIT.code.equals(statusCode)
                || CloopenStatus.TEMPLATE_NOTICE_OVER_LIMIT.code.equals(statusCode)) {
            // 请求超过平台限制
            return ExceptionCodes.SMS_REQUEST_OVER_LIMIT;
        }

        return null;
    }

}
